package pl.lodz.p.browarex.contoller;

import java.util.LinkedList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import pl.lodz.p.browarex.ws.BeerEndpoint;

@Controller
@RequestMapping("productiondetails")
public class ProductionDetailsController {
	@Inject
	private BeerEndpoint be;
	 @RequestMapping()
	public ModelAndView showLoginPage(Model model) throws Exception {
		ModelAndView mav = new ModelAndView("details");

		
        String xml = "SZCZEGOLY !";
		mav.addObject("xml", xml);
		mav.addObject("be", be);
        return mav;
	}
	
    @RequestMapping(params="operation=changeSpeed")
    public ModelAndView changeSpeed(@RequestParam("selectedId") int id, @RequestParam("speedValue") int speedValue, ModelMap model) throws Exception {
		ModelAndView mav = new ModelAndView("details");
		System.out.println("dsadafgagadagfgggfgfdsa");
		System.out.println(speedValue);
    	be.getProductionThreadsList().get(id).setSpeed(speedValue);
    	System.out.println(be.getProductionThreadsList().get(id).getSpeed());
//
//		if(speedValue==5){
//			be.getProductionThreadsList().get(id).setPause(true);
//		}else{
//			be.getProductionThreadsList().get(id).setPause(false);
//		}
		mav.addObject("be", be);
        return mav;
    }
    @RequestMapping(params="operation=pauseTheard")
    public ModelAndView pauseProduction(@RequestParam("selectedId") int id, ModelMap model) throws Exception {
		ModelAndView mav = new ModelAndView("details");
		
    	System.out.println("dsadasdasdadad"+be.getProductionThreadsList().get(id).isPause());
    	
		if(be.getProductionThreadsList().get(id).isPause()){
			be.getProductionThreadsList().get(id).setPause(false);
		}else{
			be.getProductionThreadsList().get(id).setPause(true);
		}
    	
		mav.addObject("be", be);
        return mav;
    }
    @RequestMapping(params="operation=addTheard")
    public ModelAndView addTheard( ModelMap model) throws Exception {
		ModelAndView mav = new ModelAndView("details");
		
		be.addTheard(be.getProductionThreadsList().size());
    	
		mav.addObject("be", be);
        return mav;
    }
    @RequestMapping(params="operation=removeTheard")
    public ModelAndView removeProduction(@RequestParam("selectedId") int id, ModelMap model) throws Exception {
		ModelAndView mav = new ModelAndView("details");
		
		be.getProductionThreadsList().remove(id);
		for(int i =0; i< be.getProductionThreadsList().size();i++){
			be.getProductionThreadsList().get(i).setNumber(i);
		}
    	
		mav.addObject("be", be);
        return mav;
    }
}
